<!-- 列表数据 -->
<template>
  <div class="system-download-list-data-table" v-loading="loading">
    <div class="header clearfix">
      <div class="tb-opt">
        <el-button type="text" @click="deleteDownloads">批量删除</el-button>
        <el-button type="text" @click="updateCategory">批量修改分类</el-button>
        <p class="tip-color1" style="font-size:12px;">排序根据：数值倒序排列，可点击选中修改！</p>
      </div>
      <div class="toppage">
        <PageTurning :page="page" :changePage="changePage" />
      </div>
    </div>
    <DataList :dataTable="dataTable" :data="data" :multipleSelection="multipleSelection"
      :multipleSelectionChange="multipleSelectionChange" :resetData="resetData" :system="system"
      :setDataTable="setDataTable" :page="page"/>
    <div class="footer">
      <PageTurning :page="page" :changePage="changePage" />
    </div>
    <div style="clear:both;"></div>
    <UpdateCategory ref="updateCategory" :resetData="resetData"/>
  </div>
</template>

<script>
import PageTurning from '@/components/common/PageTurning.vue'
import DataList from '@/components/main/user/system/download/DataList.vue'
import UpdateCategory from '@/components/main/user/system/category/common/UpdateCategory.vue'
export default {
  created () {
    let query = this.$route.query
    if (!query || !query.sid) {
      this.$message.error('未能获取到正确的数据')
      return
    }
    this.systemId = parseInt(query.sid)
  },
  props: {
    search: {
      type: Object
    },
    dataTotal: {
      type: Function
    },
    system: {
      type: Object
    }
  },
  mounted () {
    // 判断是否从编辑页返回获取编码
    let systbPageCurInfo = JSON.parse(sessionStorage.getItem('systbPageCur' + this.systemId))
    if (systbPageCurInfo && systbPageCurInfo.fromItem) {
      this.page.page = systbPageCurInfo.pageCur
    }
    // 请求获取数据
    this.resetData()
  },
  data () {
    return {
      // 加载中
      loading: false,
      // 系统id
      systemId: 0,
      // 非回收站内的数据总数
      total: 0,
      page: {
        // 当前页
        page: 1,
        // 一页大小
        size: 12,
        // 总数
        total: 0
      },
      // 请求时获取的数据
      data: [],
      // 表格显示的数据
      dataTable: [],
      // 批量删除选中部分
      multipleSelection: []
    }
  },
  methods: {
    multipleSelectionChange (val) {
      this.multipleSelection = val
    },
    resetData (page) {
      if (typeof page !== 'undefined') {
        this.page = Object.assign({}, this.page, page)
      }
      // 获取数据
      let param = Object.assign({ systemId: this.systemId }, this.search, this.page)
      if (!param.createAt) {
        param.createAt = []
      }
      this.loading = true
      this.$jpost('/system/download/table-data-list', param).then(response => {
        if (response) {
          if (response.total) {
            this.page.total = response.total
          } else {
            this.page.total = 0
          }
          this.dataTotal(this.page.total)
          if (response.list.length > 0) {
            this.dataTable = response.list
            this.data = JSON.parse(JSON.stringify(this.dataTable))
          } else {
            this.dataTable = []
            this.data = []
          }
        }
        this.loading = false
      })
    },
    // 改变页面
    changePage (page) {
      this.page = JSON.parse(JSON.stringify(page))
      this.resetData()
    },
    // 批量删除
    deleteDownloads () {
      if (this.multipleSelection.length < 1) {
        this.$message.error('请选择要删除的数据！')
      } else {
        let param = []
        for (let i in this.multipleSelection) {
          param.push(this.multipleSelection[i].id)
        }
        this.$confirm('确认要删除选中的' + param.length + '个下载项？', '提示', {
          type: 'warning'
        })
          .then(() => {
            this.$jpost('/system/download/info-deletes', param).then(response => {
              if (response && response.length > 0) {
                this.resetData()
              }
            })
          })
          .catch(() => {
          })
      }
    },
    // 重置dataTable
    setDataTable (dataTable) {
      this.dataTable = dataTable
    },
    // 批量更改分类
    updateCategory () {
      if (this.multipleSelection.length < 1) {
        this.$message.error('请选择要更改分类的数据！')
      } else {
        let param = []
        for (let i in this.multipleSelection) {
          param.push(this.multipleSelection[i].id)
        }
        this.$refs.updateCategory.show(this.system, param)
      }
    }
  },
  beforeDestroy () {
    // 存储系统页面页码，如果从编辑页返回列表页fromItem改为true
    let systbPageCur = JSON.parse(sessionStorage.getItem('systbPageCur' + this.systemId))
    if (systbPageCur) {
      systbPageCur.fromItem = false
      sessionStorage.setItem('systbPageCur' + this.systemId, JSON.stringify(systbPageCur))
    }
  },
  components: {
    PageTurning,
    DataList,
    UpdateCategory
  }
}

</script>
<style scoped lang="scss">
.header{
  padding: 10px;
  background: #fafafa;
  margin-bottom: 5px;
  .tb-opt{
    float: left;
  }
  .toppage{
    float: right;
    margin-top: 12px;
  }
}
.system-download-list-data-table .footer {
  float: right;
  margin: 12px 0 24px;
}
</style>
